<?php

namespace App\Filament\Resources;

use App\Filament\Resources\MusicResource\Pages;
use App\Models\Music;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Radio;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Columns\ImageColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Columns\ToggleColumn;
use Filament\Tables\Table;
use Livewire\Features\SupportFileUploads\TemporaryUploadedFile;

class MusicResource extends Resource
{
    protected static ?string $model = Music::class;

    protected static ?string $navigationIcon = 'heroicon-o-musical-note';

    // 导航项分组翻译
    public static function getNavigationGroup(): ?string
    {
        return __('menu.music_group.name');
    }

    // 菜单标题导航翻译
    public static function getNavigationLabel(): string
    {
        return __('menu.music_group.music');
    }

    // 面包屑导航翻译
    public static function getModelLabel(): string
    {
        return __('menu.music_group.music');
    }

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                TextInput::make('name')->label(__('music.Name'))->required(),
                TextInput::make('singer')->label(__('music.Singer'))->required(),
                FileUpload::make('cover_image')
                    ->label(__('music.Cover_image'))
                    ->image()
                    ->getUploadedFileNameForStorageUsing(
                        function (TemporaryUploadedFile $file) {
                            return (string)str('images/' . date('Ymd') . '/' . $file->getClientOriginalName());
                        }
                    ),
                FileUpload::make('url')
                    ->label(__('music.Url'))
                    ->getUploadedFileNameForStorageUsing(
                        function (TemporaryUploadedFile $file) {
                            return (string)str('musics/' . $file->getClientOriginalName());
                        }
                    ),
                Radio::make('status')
                    ->label(__('music.Status'))
                    ->options([
                        '0' => __('music.Status_0'),
                        '1' => __('music.Status_1'),
                    ])
                    ->inline()
                    ->inlineLabel(false)
                    ->default('1')
                    ->required(),
            ])
            ->columns(1);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                ImageColumn::make('cover_image')->label(__('music.Cover_image'))
                    ->circular()->width(100)->height(100)->alignCenter(),
                TextColumn::make('name')->label(__('music.Name'))
                    ->badge()->copyable()->alignCenter(),
                TextColumn::make('singer')->label(__('music.Singer'))
                    ->color('success')->copyable(),
                ToggleColumn::make('status')
                    ->label(__('music.Status'))
                    ->alignCenter(),

            ])
            ->filters([
                //
            ])
            ->actions([
                Tables\Actions\EditAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ]);
    }

    public static function getRelations(): array
    {
        return [
            //
        ];
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListMusic::route('/'),
        ];
    }
}
